// 镜像仓库专用配置文件
// 支持GitHub和Gitee双平台

// 检测当前平台
function detectPlatform() {
  const hostname = window.location.hostname;
  if (hostname.includes('gitee.io')) return 'gitee';
  if (hostname.includes('github.io')) return 'github';
  if (hostname.includes('vercel.app')) return 'vercel';
  return 'localhost';
}

// 获取当前平台配置
function getPlatformConfig() {
  const platform = detectPlatform();
  const configs = {
    github: {
      name: 'GitHub Pages',
      apiUrl: 'https://your-vercel-api.vercel.app', // GitHub使用Vercel API
      siteUrl: 'https://你的用户名.github.io/magazine',
      cdn: 'jsdelivr',
      speed: 'global'
    },
    gitee: {
      name: 'Gitee Pages', 
      apiUrl: 'https://your-cloud-api.com', // Gitee使用国内云API
      siteUrl: 'https://你的用户名.gitee.io/magazine',
      cdn: 'bootcdn',
      speed: 'china'
    },
    vercel: {
      name: 'Vercel',
      apiUrl: 'https://your-vercel-api.vercel.app',
      siteUrl: 'https://your-domain.vercel.app',
      cdn: 'unpkg',
      speed: 'global'
    },
    localhost: {
      name: '本地开发',
      apiUrl: 'http://localhost:3000',
      siteUrl: 'http://localhost:1313',
      cdn: 'local',
      speed: 'local'
    }
  };
  
  return configs[platform] || configs.localhost;
}

// 主配置
window.MAGAZINE_CONFIG = {
  // 基础配置
  SITE_NAME: '精英杂志',
  
  // 平台检测
  PLATFORM: detectPlatform(),
  PLATFORM_CONFIG: getPlatformConfig(),
  
  // Stripe配置
  STRIPE_PUBLISHABLE_KEY: 'pk_test_你的密钥',
  
  // 订阅配置
  SUBSCRIPTION_PRICE: 365,
  SUBSCRIPTION_CURRENCY: 'cny',
  
  // 功能开关
  INVITE_REQUIRED: true,
  FREE_ARTICLES_COUNT: 3,
  PAYMENT_ENABLED: true,
  
  // 镜像仓库配置
  MIRROR_SITES: {
    github: 'https://你的用户名.github.io/magazine',
    gitee: 'https://你的用户名.gitee.io/magazine'
  },
  
  // 智能切换
  AUTO_REDIRECT: true, // 是否根据地理位置自动切换
  
  // 调试模式
  DEBUG: true
};

// 显示平台信息
console.log('📱 当前平台:', window.MAGAZINE_CONFIG.PLATFORM);
console.log('⚙️ 平台配置:', window.MAGAZINE_CONFIG.PLATFORM_CONFIG);

// 智能推荐最优站点
function recommendSite() {
  const userLang = navigator.language || navigator.userLanguage;
  const isChinaUser = userLang.includes('zh') || 
                     window.location.hostname.includes('.cn') ||
                     navigator.language === 'zh-CN';
  
  if (isChinaUser && window.MAGAZINE_CONFIG.MIRROR_SITES.gitee) {
    return {
      recommended: 'gitee',
      reason: '检测到中文用户，推荐使用Gitee获得更快访问速度',
      url: window.MAGAZINE_CONFIG.MIRROR_SITES.gitee
    };
  } else {
    return {
      recommended: 'github',
      reason: '推荐全球用户使用GitHub',
      url: window.MAGAZINE_CONFIG.MIRROR_SITES.github
    };
  }
}

// 显示站点推荐
window.showSiteRecommendation = function() {
  const recommendation = recommendSite();
  const currentPlatform = detectPlatform();
  
  if (recommendation.recommended !== currentPlatform && 
      window.MAGAZINE_CONFIG.AUTO_REDIRECT) {
    
    const message = `
      <div class="alert alert-info alert-dismissible fade show" role="alert" style="position: fixed; top: 20px; right: 20px; z-index: 9999; max-width: 400px;">
        <h6>🚀 ${recommendation.reason}</h6>
        <p>点击切换到：<a href="${recommendation.url}" class="alert-link">${recommendation.url}</a></p>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
      </div>
    `;
    
    // 延迟显示推荐
    setTimeout(() => {
      document.body.insertAdjacentHTML('beforeend', message);
    }, 2000);
  }
};

// 显示镜像站点信息
window.showMirrorInfo = function() {
  const platform = detectPlatform();
  const mirrorSites = window.MAGAZINE_CONFIG.MIRROR_SITES;
  
  let infoHTML = '<div class="mirror-info"><h6>🔗 镜像站点</h6>';
  
  for (let [key, url] of Object.entries(mirrorSites)) {
    const isCurrent = platform === key;
    const status = isCurrent ? '✅ 当前' : '🔗 访问';
    infoHTML += `
      <div class="mirror-site ${isCurrent ? 'current' : ''}">
        <strong>${key.toUpperCase()}:</strong> 
        <a href="${url}" target="_blank">${url}</a> 
        <span class="status">${status}</span>
      </div>
    `;
  }
  
  infoHTML += '</div>';
  
  // 添加到页面底部
  const footer = document.querySelector('footer') || document.body;
  footer.insertAdjacentHTML('beforeend', `
    <div class="container mt-5">
      ${infoHTML}
    </div>
  `);
};

// 初始化
console.log('🔄 镜像仓库配置已加载');
console.log('📊 站点推荐:', recommendSite());